const app = getApp()
var that;
var util = require('../../utils/util.js');
Page({
  /* 页面的初始数据*/
  data: {
    DataARRY:[],
    userId:'',
    total:0,
    userInfo:'',
    nowPage:1,
    reachBottomLoading:false,
  },

  onLoad: function (options) {
    that = this;

    // 判断登陆状态
    if (app.globalData.checkLogin) {
      console.log('判断onLaunch是否执行完毕1')
      if (app.globalData.loginUserObj != null) {
        that.setData({
          userId: app.globalData.loginUserObj.id,
          userInfo: app.globalData.loginUserObj
        })
        //获取用户信息
        that.getuserlist();
      }
    } else {
      app.checkLoginReadyCallback = res => {
        console.log('判断onLaunch是否执行完毕2', res)
        if (app.globalData.loginUserObj != null) {
          that.setData({
            userId: app.globalData.loginUserObj.id,
            userInfo: app.globalData.loginUserObj
          })
          //获取用户信息
          that.getuserlist();
        }
      };
    }
  },

  //获取
  getuserlist: function () {
    that = this; 
    wx.request({
      url: app.http + 'hysUser/memberList',
      data: {
        companyId: that.data.userInfo.ydCompany.id,
        nowPage:that.data.nowPage,
        pageSize:10,
      },
      method: 'GET',
      header: {
        'content-type': 'application/json', // 默认值
        'APP-User-Token': app.globalData.tokenMap.access_token
      },
      success: function (res) {
        console.log("suc返回", res)
        if (res.data.code == 200) {
          if (that.data.nowPage==1){
            that.setData({
              DataARRY: res.data.data.list,
              total: res.data.data.total,
              reachBottomLoading: false,
            })
          }else{
            let dataA = that.data.DataARRY
            dataA.concat(res.data.data.list)
            that.setData({
              DataARRY: dataA,
              total: res.data.data.total,
              reachBottomLoading: false,
            })
          }
      
          wx.hideLoading();
          wx.stopPullDownRefresh()
        }
        else if (res.data.code == 100101) {
          // token无效，获取新的token
          var newToken = util.refreshTokenFun(app.globalData.tokenMap.refresh_token).then(
            function (res) {
              if (res.data.code == 200) {
                that.getuserlist();
              }
              else if (res.data.code == 400) {
                var wxLoginFun = util.wxLoginFun().then(
                  function (res2) {
                    if (res2.data.code == 200) {
                      that.getuserlist();
                    }
                  }
                )
              }
            }
          ).catch(
            function (err) {
              console.log("获取新的token失败返回", err)
            }
          )
        }
      },
      fail: function (err) {
        console.log("fail返回", err)
      }
    })
  },

  /*监听用户下拉动作处理函数*/
  onPullDownRefresh: function () { 
    console.log(2)
    wx.showLoading({
      title: '加载中...',
      mask: true
    })
    that.setData({
      reachBottomLoading: true,
      nowPage: 1
    })
    this.getuserlist();
  },

  /* 页面上滑触底事件的处理函数 */
  onReachBottom: function () {
    console.log(1)
    if (that.data.reachBottomLoading == false) {
      var myCount = that.data.nowPage * 3;
      if (myCount < that.data.total) {
        wx.showLoading({
           title: '加载中...',
           mask: true     
        })
        that.data.nowPage++;
        that.setData({
          reachBottomLoading: true,
          nowPage: that.data.nowPage
        })
        that.getuserlist();
      } else {
        wx.showToast({
          title: '已加载全部',
          icon: 'none'
        })
      }
    }

  },

})